package org.barman.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.barman.exception.CustomMessageException;

/**
 * Servlet implementation class ExceptionHandlingServlet
 */
abstract public class ExceptionHandlingHttpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ExceptionHandlingHttpServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		try {
			doExceptionHandledPost(request, response);
		} catch (CustomMessageException e) {
			// e.printStackTrace();

			request.setAttribute("flash.message", e.getCustomMessage());
			response.sendRedirect(request.getHeader("Referer"));
		}

	}

	abstract protected void doExceptionHandledPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, CustomMessageException;

}
